<template>
  <div>
    <!--    编辑/添加表单-->
    <!--    编辑/添加表单-->
    <!--    编辑/添加表单-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%" resetFields>
      <el-form :model="returnListAll" :rules="unitMeasureRules" ref="unitMeasureRulesForm">
        <el-form-item label="orderId" prop="orderId" v-show="false ">
          <el-input v-model="returnListAll.orderId" autocomplete="on"></el-input>
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="returnListAll.country" autocomplete="on" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="returnListAll.orderNumber" autocomplete="on" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-input v-model="returnListAll.category" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="returnListAll.brand" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="returnListAll.quantity" autocomplete="on" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="发货仓库编码" prop="warehouseCode">
          <el-input v-model="returnListAll.warehouseCode" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="退货原因" prop="returnReason">
          <el-input v-model="returnListAll.returnReason" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="订购时间" prop="orderPlacedAt">
          <el-date-picker
            v-model="returnListAll.orderPlacedAt"
            align="right"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="买家备注" prop="buyerRemarks">
          <el-input v-model="returnListAll.buyerRemarks" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="returnStatus">
          <el-radio-group v-model="returnListAll.returnStatus">
            <el-radio label="1">已取消</el-radio>
            <el-radio label="2">已退货</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMeasure('unitMeasureRulesForm')">确 定</el-button>
      </div>

    </el-dialog> <!--    功能-->
    <!--    功能-->
    <!--    功能-->
    <el-button size="small" type="danger" icon="el-icon-delete" @click="returnDelMore">删除</el-button>
    <el-button size="small" type="primary" icon="el-icon-edit" @click="returnOrderAdd">添加</el-button>

    <!--    表单-->
    <!--    表单-->
    <!--    表单-->
    <el-table v-loading="loading" :data="returnList" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="国家" align="center" prop="country"/>
      <el-table-column label="订单号" align="center" prop="orderNumber"/>
      <el-table-column label="分类" align="center" prop="category"/>
      <el-table-column label="品牌" align="center" prop="brand"/>
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="发货仓库编码" align="center" prop="warehouseCode"/>
      <el-table-column label="退货原因" align="center" prop="returnReason"/>
      <el-table-column label="订购时间" align="center" prop="orderPlacedAt"/>
      <el-table-column label="买家备注" align="center" prop="buyerRemarks"/>
      <el-table-column label="状态" align="center" prop="returnStatus"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="returnUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="returnDelete(scope.row)"
          >删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <span class="demonstration"/>
    <el-pagination
      style="margin-right: 14px; margin-top: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 7, 10,15,20]"
      :page-size="100"
      layout=" ->, total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {returnAddFind, returnGetList, returnListDelMore, returnUpMeasure,returnDelById } from "@/api/returnList"


export default {
  data() {
    return {
      // -----
      ids:[],
      // ----
      title: '',
      // 编辑,数据回显
      // 编辑,数据回显
      // 编辑,数据回显
      returnListAll: {
        orderId: null,
        country: null,
        category: null,
        orderNumber: null,
        brand: null,
        quantity: null,
        warehouseCode: null,
        returnReason: null,
        orderPlacedAt: null,
        buyerRemarks: null,
        returnStatus: null,
      },
      // 是否显示更新&添加表单
      dialogFormVisible: false,
      // 更新&添加表单规则
      unitMeasureRules: {
        orderNumber: [
          {required: true, message: '请输入订单号', trigger: 'blur'}
        ],
        category: [
          {required: true, message: '请输入', trigger: 'blur'}
        ],
        country: [
          {required: true, message: '请输入国家名称', trigger: 'blur'}
        ],
        returnReason: [
          {required: true, message: '请输入退货原因', trigger: 'blur'}
        ],
      },
      // loading
      loading: true,
      //   表单数据
      returnList: [],
      // 分页
      pageNum: 1,
      currentPage: 1,
      total: 0,
      pageSize: 5,
    }
  },
  methods: {
    fetchData() {
      let params = {
        pageNum: this.pageNum,// 分页
        pageSize: this.pageSize,// 每页数据数量

      }
      returnGetList(params).then(res => {

        let pageInfo = res.data
        this.loading = false
        this.returnList = pageInfo.list
        this.listData = pageInfo.list
        this.total = pageInfo.total
      })
    }, // 多选框ID
    // 多选框ID
    // 多选框ID
    handleSelectionChange(selection) {
      this.ids = []
      for (let i = 0; i < selection.length; i++) {
        this.ids.push(selection[i].orderId)
      }
    },
    // 分页
    // 分页
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchData()
    },
    //   删除
    //   删除
    returnDelete(row) {
      this.$confirm('此操作将永久删除 ' + row.orderNumber + ' , 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id: row.orderId
        }
        returnDelById(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    // 编辑
    returnUpdate(row) {

      this.returnListAll = row
      this.dialogFormVisible = true
      this.title = '编辑'
    },
    // 添加
    // 添加
    // 添加
    returnOrderAdd() {
      this.returnListAll = {}
      this.dialogFormVisible = true
      this.title = '添加'
    },
    // 更新，添加
    // 更新，添加
    updateMeasure(forName) {
      this.$refs[forName].validate((valid) => {
        if (valid) {
          if (this.title === "编辑") {
            returnUpMeasure(this.returnListAll).then(res => {
              alert('编辑成功!')
              this.fetchData()
            })
          } else if (this.title === "添加") {
            returnAddFind(this.returnListAll).then(res => {
              alert('添加成功!')
              this.fetchData()
            })
          }
        } else {
          return false
        }
        this.dialogFormVisible = false
      })
    },
    // 批量删除
    // 批量删除
    returnDelMore() {
      this.$confirm('此操作将永久删除 ' + this.ids + ' 单位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          ids: this.ids.join(',')
        }
        returnListDelMore(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

  },

  created() {
    this.fetchData()
  },
}
</script>

<style scoped lang="scss">


</style>
